/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.dei.perla.parser.grammar.context;

import org.dei.perla.parser.clauses.ExecuteIfClause;
import org.dei.perla.parser.clauses.HavingClause;
import org.dei.perla.parser.clauses.LLEveryClause;
import org.dei.perla.parser.clauses.LLGroupByClause;
import org.dei.perla.parser.clauses.LLGroupByField;
import org.dei.perla.parser.clauses.LLGroupByTS;
import org.dei.perla.parser.clauses.OnEmptySelectionClause;
import org.dei.perla.parser.clauses.PilotJoinClause;
import org.dei.perla.parser.clauses.SamplingClause;
import org.dei.perla.parser.clauses.SelectClause;
import org.dei.perla.parser.clauses.TerminateAfterClause;
import org.dei.perla.parser.clauses.UpToClause;
import org.dei.perla.parser.grammar.Token;
import org.dei.perla.parser.util.Duration;

/**
 * 
 * Classe contenente i parametri degli statement di basso livello del contesto corrente durante il parsing  
 *
 */
public class LLContext {

	/**
	 * Oggetto LLEveryClause corrente
	 */
	public static LLEveryClause everyClause = null;
	
	/**
	 * Oggetto SelectClause corrente
	 */
	public static SelectClause selectClause = null;
	
	/**
	 * Oggetto LLGroupByClause corrente
	 */
	public static LLGroupByClause groupByClause = null;
	
	/**
	 * Oggetto UpToClause corrente
	 */
	public static UpToClause upToClause = null;
	
	/**
	 * Oggetto HavingClause corrente
	 */
	public static HavingClause havingClause = null;
	
	/**
	 * Oggetto OnEmptyClauseClause corrente
	 */
	public static OnEmptySelectionClause emptyClause = null;
	
	/**
	 * Oggetto SamplingClause corrente
	 */
	public static SamplingClause samplingClause = null;
	
	/**
	 * Oggetto PilotJoinClause corrente
	 */
	public static PilotJoinClause pilotJoinClause = null;
	
	/**
	 * Oggetto ExecuteIfClause corrente
	 */
	public static ExecuteIfClause executeIfClause = null;
	
	/**
	 * Oggetto TerminateAfterClause corrente
	 */
	public static TerminateAfterClause terminateAfterClause = null;
	
	/**
	 * Resetta il contesto
	 */
	public static void clear()
	{
		everyClause = null;
		selectClause = null;
		groupByClause = null;
		upToClause = null;
		havingClause = null;
		emptyClause = null;
		pilotJoinClause = null;
		samplingClause = null;
		executeIfClause = null;
		terminateAfterClause = null;
	}
	
	/**
	 * Aggiunge un Field alla clausola GROUP BY
	 * @param groupByFieldToken Token con i dati del Field
	 */
	public static void addLLGroupByField(Token groupByFieldToken)
	{
		if(groupByClause == null)
			groupByClause = new LLGroupByClause();
		
		LLGroupByField groupByField = new LLGroupByField();
		String fieldName = groupByFieldToken.image;
		
		groupByField.setFieldName(fieldName);
		
		groupByClause.getGroupByFields().add(fieldName, groupByField);
	}
	
	/**
	 * Aggiunge un raggruppamento per timestamp
	 * @param interval Oggetto Duration associato 
	 * @param groups Numero di gruppi
	 */
	public static void addLLGroupByTS(Duration interval, int groups)
	{
		if(groupByClause == null)
			groupByClause = new LLGroupByClause();
		
		LLGroupByTS groupByTS = new LLGroupByTS();
		groupByTS.setGroups(groups);
		groupByTS.setInterval(interval);
		
		groupByClause.setGroupByTS(groupByTS);
	}
}
